<template>
  <div id="customers-costcenters" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1">
          <router-link to="/h5/customers">企业客户</router-link>
        </li>
        <li class="breadcrumb-item me-1 active" aria-current="page">
          订单备注
        </li>
      </ol>
    </nav>
    <div class="card-body">
      <form>
        <div class="row mb-1">
          <my-select-customer
            :customerId.sync="customerId0"
            :minId="-1"
          ></my-select-customer>
        </div>
        <div class="row mb-1">
          <select class="form-select" v-model.number="noteType0">
            <option value="0">所有</option>
            <option value="100">成本中心</option>
            <option value="200">项目名称</option>
          </select>
        </div>
        <div class="row mb-1">
          <button type="button" class="btn btn-primary" @click="search()">
            查找
          </button>
        </div>
        <div class="row mb-1">
          <button
            type="button"
            class="btn btn-secondary btn-sm"
            title="清空查找条件"
            @click="reset()"
          >
            重置
          </button>
        </div>
        <div class="row mb-1">
          <a
            href="javascript:void(0)"
            v-on:click="newNote()"
            class="btn btn-success float-end"
            >新建</a
          >
        </div>
      </form>
    </div>
    <table class="table table-striped table-sm small">
      <thead>
        <tr>
          <th>公司</th>
          <th>类型</th>
          <th>名称</th>
          <th class="d-none d-md-table-cell">创建时间</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(info, index) in dataList">
          <td>{{ info.customer.vipName }}</td>
          <td>
            <template v-if="info.noteType === 100">成本中心</template>
            <template v-if="info.noteType === 200">项目名称</template>
          </td>
          <td>{{ info.name }}</td>
          <td class="d-none d-md-table-cell">{{ info.createTime }}</td>
          <td>
            <button
              class="btn btn-danger btn-sm"
              @click="deleteCustomerNote(info.id)"
            >
              删除
            </button>
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-customer-order-note
      ref="noteModal"
    ></my-modal-customer-order-note>
  </div>
</template>

<script>
import {
  searchCustomerNotes,
  createCustomerNote,
  deleteCustomerNote,
} from "@/api/customer.js";
import $ from "jquery";
import MyPagination from "@/components/my-pagination.vue";
import MySelectCustomer from "@/components/my-select2-customer.vue";
import MyModalCustomerOrderNote from "@/components/modal/my-modal-customer-order-note.vue";

export default {
  components: {
    MyPagination,
    MySelectCustomer,
    MyModalCustomerOrderNote,
  },
  data() {
    return {
      id: 0,
      customerId: 0,
      noteType: 100,
      name: "",

      dataList: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },
      customerId0: -1,
      noteType0: 0,
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        customerId: this.customerId0,
        noteType: this.noteType0,
      };

      searchCustomerNotes(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    newNote: function () {
      this.$refs.noteModal.modal().then((v) => this.saveNote(v));
    },
    saveNote: function (params) {
      createCustomerNote(params, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg("操作失败：" + jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("操作成功");
          this.search();
        }
      });
    },
    deleteCustomerNote: function (id) {
      deleteCustomerNote(id, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg("操作失败：" + jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("操作成功");
          this.search();
        }
      });
    },
    reset: function () {
      this.name = "";
      this.sc.pageNo = 1;
      this.customerid0 = -1;
      this.noteType0 = 0;
      this.search();
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>